//
//  Activity.m
//  EFSMobile
//
//  Created by CaoJun on 15/11/20.
//  Copyright © 2015年 Elephants Financial Service. All rights reserved.
//

#import "ActivityInfo.h"
#import "YSUserManager.h"
//#import "EFSServiceManager.h"
#import <YTXRequest/YTXRequest.h>


@implementation ActivityInfo


- (NSNumber *)shareEnabled
{
    if(_shareEnabled == nil) {
        _shareEnabled = @(YES);
    }
    return _shareEnabled;
}

- (NSDate *)date {
    return [NSDate dateWithTimeIntervalSince1970:[self.publishTime longValue] / 1000];
}

- (BOOL)hasImage{
    return self.img && self.img.description.length > 7;
}

+ (NSDictionary *)JSONKeyPathsByPropertyKey
{
    return @{
             @"activityId": @"id",
             @"shareEnabled": [NSNull null]
             };
}

+ (NSValueTransformer *)imgJSONTransformer
{
    return [MTLValueTransformer valueTransformerForName:MTLURLValueTransformerName];
}

+ (NSValueTransformer *)shareImgJSONTransformer
{
    return [MTLValueTransformer valueTransformerForName:MTLURLValueTransformerName];
}


+ (NSValueTransformer *)galleryImgJSONTransformer
{
    return [MTLValueTransformer valueTransformerForName:MTLURLValueTransformerName];
}


+ (NSValueTransformer *)urlJSONTransformer
{
    return [MTLValueTransformer valueTransformerForName:MTLURLValueTransformerName];
}

#pragma network
+ (RACSignal *)activityInfoById:(NSString *)activityId
{
    RACReplaySubject *subject = [RACReplaySubject subject];
    NSDictionary *params = @{@"serverId" : @"3004",
                             @"token" : objectOrEmptyStr([YSUserManager sharedManager].currentUser.token),
                             @"ids": objectOrEmptyStr(activityId)
                             };
    YSRequest *request = [YSRequest requestWithName:@"activity.activityById"];
    [request sendWithParameters:params success:^(id response) {
        ActivityInfo *activity = [MTLJSONAdapter modelOfClass:[ActivityInfo class] fromJSONDictionary:[response[@"articles"] firstObject] error:NULL];
//        EFSLogH(request.requestUrl, request.requestMethod, params, [response[@"articles"] firstObject], nil);
        [subject sendNext:activity];
        [subject sendCompleted];
    } failure: ^(YSRequest *request){
        NSError *error = [NSError errorWithDomain:@"EFS" code:0 userInfo:@{EFS_ERROR_MESSAGE_KEY:@"活动信息获取失败"}];
//        EFSLogH(request.requestUrl, request.requestMethod, params, nil, error);
        [subject sendError:nil];
    }];
    return subject;
}


- (RACSignal *)participate
{
    RACReplaySubject *subject = [RACReplaySubject subject];
//    NSDictionary *params = @{
//                             @"Participate": @{
//                                 @"activityId": objectOrEmptyStr(self.activityId),
//                                 @"username": objectOrEmptyStr([EFSUserManager sharedManager].currentUser.username)
//                             }
//                            };
//    YTXRequest *request = [YTXRequest requestWithName:@"activity.participate"];
//    request.requestSerializerType = YTXRequestSerializerTypeJSON;
//    [request sendWithParameters:params success:^(id response) {
//        EFSLogH(request.requestUrl, request.requestMethod, params, response, nil);
//        [subject sendNext:nil];
//        [subject sendCompleted];
//    } failure: ^(YTXRequest *request){
//        NSError *error = [NSError errorWithDomain:@"EFS" code:0 userInfo:@{EFS_ERROR_MESSAGE_KEY:@"参加活动失败"}];
//        EFSLogH(request.requestUrl, request.requestMethod, params, nil, error);
//        [subject sendError:nil];
//    }];
    return subject;
}

@end
